var menuDialog = null;
var menusForm = null;
var treegrid;
$(function() {
	treegrid = $('#menuList').treegrid(
			{
				title : '',
				url : 'menu/menuList',
				idField : 'id',
				treeField : 'text',
				parentField : 'pid',
				rownumbers : true,
				striped : true,
				frozenColumns : [ [ {
					field : "chk",
					title : "",
					align : "center",
					width : 100,
					checkbox : true
				}, {
					width : '60',
					title : '菜单编号',
					field : 'id',
					hidden : true
				}, {
					width : '200',
					title : '菜单名称',
					field : 'text'
				} ] ],
				columns : [ [
						{
							width : '200',
							title : '图标名称',
							field : 'iconCls'
						},
						{
							width : '200',
							title : '菜单路径',
							field : 'url',
							formatter : function(value, row) {
								if (row.attributes.url == null) {
									return "";
								} else {
									return sy.formatString(
											'<span title="{0}">{1}</span>',
											row.attributes.url,
											row.attributes.url);
								}
							}
						}, {
							width : '150',
							title : '菜单类型',
							field : 'type',
							formatter : function(value, row) {
								if (row.attributes.type == 0) {
									return "菜单";
								} else {
									return "功能";
								}
							}
						}, {
							width : '200',
							title : '菜单描述',
							field : 'attributes',
							formatter : function(value, row) {
								return row.attributes.menu_desc;
							}
						}, {
							width : '100',
							title : '顺序',
							field : 'sort'
						} ] ],
				toolbar : '#toolbar',
				onLoadSuccess : function(row, data) {
					$('.iconImg').attr('src', sy.pixel_0);
					parent.$.messager.progress('close');
				}
			});

});

// 新增
var addFun = function() {
	var dialog = parent.sy.modalDialog({
		title : '新增菜单信息',
		url : "menu/toaddmenu",
		buttons : [ {
			text : '保存',
			handler : function() {
				dialog.find('iframe').get(0).contentWindow.saveFun(dialog,
						treegrid, parent.$);
			}
		} ]
	});
};

// 修改
var editFun = function() {
	var rows = treegrid.treegrid('getSelections');
	if (rows.length <= 0) {
		$.messager.show({
			msg : '请选择要编辑的行!',
			title : '提示'
		});
		return;
	} else {
		var dialog = parent.sy.modalDialog({
			title : '编辑菜单信息',
			url : 'menu/toaddmenu?menuid=' + rows[0].id,
			buttons : [ {
				text : '编辑',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.editF(dialog,
							treegrid, parent.$);
				}
			} ]
		});
	}

};

// 删除
var removeFun = function() {
	var rows = treegrid.treegrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
			if (r) {
				$.ajax({
					url : "menu/delmenu",
					data : {
						menuids : rows[0].id
					},
					cache : false,
					dataType : "json",
					success : function(data) {
						if (data.success) {
							treegrid.treegrid('unselectAll');
							treegrid.treegrid('reload');
							$.messager.show({
								msg : data.msg,
								title : '提示'
							});
						}
					}
				});
			}
		});
	} else {
		$.messager.show({
			title : '提示',
			msg : '请选择要删除的记录！'
		});
	}
};

// 展开
var redoFun = function() {
	treegrid.treegrid('expandAll');
};
// 折叠
var undoFun = function() {
	treegrid.treegrid('collapseAll');
};

// 刷新
var reload = function() {
	treegrid.treegrid('reload');
};
